#import <Foundation/Foundation.h>
#import "TTTableNeedItem.h"
#import "TTTableNeedItemCell.h"
#import "TTTableMyNeedsItem.h"
#import "TTTableMyNeedsItemCell.h"
#import "TTTableHelpItem.h"
#import "TTTableHelpItemCell.h"
#import "INDefaultStylesheet.h"
#import "UsersViewDataSource.h"
#import "UsersModel.h"
#import "Need.h"


#define POST_ROW_HEIGHT         120
#define kSectionHeaderHight     25.0

@implementation UsersViewDataSource

///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (void)tableViewDidLoadModel:(UITableView *)tableView
{
	
    [super tableViewDidLoadModel:tableView];
	[tableView setEditing:NO animated:YES];
	
    [self.items removeAllObjects];
	
    // Construct an object that is suitable for the table view system
    // from each SearchResult domain object that we retrieve from the TTModel.
    for (User *result in [(id<UsersModel>)self.model results]) {
		
		//NSString *needUrl = [NSString stringWithFormat:@"in://needView/?need=%@", result._id];
        [self.items addObject:[TTTableHelpItem itemWithText:[TTStyledText textFromXHTML:result.nickname]
												displayname:[NSString stringWithFormat:@"%@ %@", result.name, result.lastname]
												  timestamp:[NSDate date]
												 pictureurl:result.pictureurl
														URL:nil]];
		
	    for (Need *need in result.Needs) {
			//NSString *userUrl = [NSString stringWithFormat:@"in://userView/?user=%@", help.user._id];
			NSString *needUrl = [NSString stringWithFormat:@"in://needView/?need=%@", need._id];
			[self.items addObject:[TTTableMessageItem itemWithTitle:need.subject 
															caption:need.city
															   text:need.text 
														  timestamp:[NSDate dateWithTimeIntervalSinceNow:[need.timediff doubleValue]]
																URL:needUrl]];
			
			//TTLOG(@"Added %@ need ", help._id);
		}
	
	}
	
//    NSLog(@"Added %lu search result objects", (unsigned long)[self.items count]);
}

///////////////////////////////////////////////////////////////////////////////////////////////////

- (void)dealloc {
	[super dealloc];
}

- (Class)tableView:(UITableView*)tableView cellClassForObject:(id) object { 
	
	if ([object isKindOfClass:[TTTableHelpItem class]]) { 
		return [TTTableHelpItemCell class]; 		
	} else if ([object isKindOfClass:[TTTableMyNeedsItem class]]) { 
		return [TTTableMyNeedsItemCell class]; 		
	} else
	{ 
		return [super tableView:tableView cellClassForObject:object]; 
	}
//	
//	if ([object isKindOfClass:[TTTableHelpItem class]]) { 
//		return [TTTableHelpItemCell class]; 		
//	} else { 
//		return [super tableView:tableView cellClassForObject:object]; 
//	}
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return NO;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	
}


- (void)tableView:(UITableView*)tableView prepareCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//	cell.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);

//	cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//	cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];

}

@end
